package com.opengroupe.dao.impl;

// Generated 31 juil. 2012 15:44:44 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.opengroupe.dao.QuestionDao;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.Question;

/**
 * Home object for domain model class Question.
 * @see com.opengroupe.entities.Question
 * @author Hibernate Tools
 */
@Stateless
public class QuestionDaoImpl extends JpaDaoSupport implements QuestionDao {

	private static final Log log = LogFactory.getLog(QuestionDaoImpl.class);

	public Question persist(Question transientInstance) {
		log.debug("persisting Question instance");
		try {
			getJpaTemplate().persist(transientInstance);
			log.debug("persist successful");
			return transientInstance;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(Question persistentInstance) {
		log.debug("removing Question instance");
		try {
			getJpaTemplate().remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Question merge(Question detachedInstance) {
		log.debug("merging Question instance");
		try {
			Question result = getJpaTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Question findById(Integer id) {
		log.debug("getting Question instance with id: " + id);
		try {
			Question instance = getJpaTemplate().find(Question.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Question> findAll() {
		log.debug("finding all Questions");
		try {
			List<Question> questions = (List<Question>) getJpaTemplate().find("from Question");
			log.debug("findAll successful");
			return questions;
		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			throw re;
		}
	}

	@Transactional
	public void deleteCascade(final Question tempQuestion) {
		
		JpaCallback<Candidat> jpaCallback = new JpaCallback<Candidat>() {

			@Override
			public Candidat doInJpa(EntityManager entityManager)
					throws PersistenceException {
				
				// Supprimer les CRQ
				String delCRQ = "delete CandidatReponseQuestion as crq where " +
						"crq.question.idQuestion=some(select q.idQuestion from " +
						"Question as q where q.idQuestion=" + tempQuestion.getIdQuestion() + ")";
				Query query = entityManager.createQuery(delCRQ);
				query.executeUpdate();
				
				return null;
			}
		};
		getJpaTemplate().execute(jpaCallback);
		remove(findById(tempQuestion.getIdQuestion()));
	}
}
